$.showLoading();
thin.imports('/public/configure/conf.minfo.js', function() {
	factory.request('_minfo_get');
});

thin.imports('/public/plug/JQueryWeui/city-picker.min.js', function() {
	$("#btn-city").on('click', function(e) {
		var e = e || window.event;
		e.stopPropagation();
		e.preventDefault();
		$("#btn-city").picker('open');
		return false;
	}).cityPicker({
		title: '选择城市',
		showDistrict: false,
		onClose: function(res) {
			var d = res.displayValue;
			$('#ipt-mi-province').val(d[0]);
			$('#ipt-mi-city').val(d[1]);
			$('#btn-city').find('.value-text').html(d[0] + '  ' + d[1]);
		}
	});
});

function __complete_minfoget(res) {
	$.hideLoading();
	if(res.status == 200) {
		jude_minfo = true;
		var data = res.data,
			province = '',
			city = '',
			headimgurl = '';
		for(var i in data) {
			$('#ipt-mi-' + i).val(data[i]);
			(i == 'province') && (province = data[i]);
			(i == 'city') && (city = data[i]);
			(i == 'headimgurl') && (headimgurl = data[i]);
		}
		$('#ipt-mobile').val(data.mobile);
		(headimgurl != '') && $('#bind-headimgurl').attr('src', headimgurl);
		$('#btn-city').find('.value-text').html(province + '  ' + city);
	} else {
		$.toptip(res.error, 'error');
	}
}

var __uheadimgurl = {
	'imgelem': function() {
		return document.getElementById('bind-headimgurl');
	},
	'process': function() {
		
	},
	'complete': function(res) {
		if(res.status == 201) {
			$('#ipt-mi-headimgurl').val(res.data.uri);
		} else {
			$.toptip(res.error, 'error');
		}
	}
}

$(function() {
	$('#btn-memberinfo').on('click', function(e) {
		submitMemberInfo();
	});
});

var __getdata_minfocomplete = {};

function submitMemberInfo() {
	$('[id*=ipt-mi]').each(function(index) {
		var that = $(this),
			name_arr = that.attr('id'),
			name = name_arr.split('-')[2];
		__getdata_minfocomplete[name] = that.val();
	});
	if(__getdata_minfocomplete['headimgurl'] == '' || __getdata_minfocomplete['nickname'] == '') {
		$.showLoading();
		factory.request('_m_wxinfo', function() {
			$.showLoading();
			factory.request('_minfo_complete');
		});
	} else {
		$.showLoading();
		factory.request('_minfo_complete');
	}
}

function __complete_mwxinfo(res, unkn) {
	$.hideLoading();
	if(res.status == 200) {
		__getdata_minfocomplete['headimgurl'] = res.data.headimgurl;
		__getdata_minfocomplete['nickname'] = res.data.nickname;
	}
	unkn();
}

function __complete_minfocomplete(res) {
	$.hideLoading();
	if(res.status == 201) {
		if(typeof data_role != 'undefined' && (data_role == 2 || data_role == 3)) {
			checkLocation('memberlike');
		} else {
			$.toast("信息完善成功", 960);
			setTimeout(function() {
				window.location.href = '/mobile/service/member';
			}, 800);
		}
	} else {
		$.toptip(res.error, 'error');
	}
}